package com.cy.sca.controller;

import com.cy.sca.common.CommonResult;
import com.cy.sca.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <br>@class: OrderController
 * <br>@description: mall系统order模块处理
 * <br>@author: cy
 * <br>@date: 2020-04-18 11:46:53
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${nacos.mall.services.order}")//从yml中读取
    private String serverURL;

    @PostMapping("/add")
    public CommonResult addOrder(Order order) {
        log.info("order => " + order);
        CommonResult result = new CommonResult().setCode(444).setMessage("添加失败");
        if (restTemplate.postForObject(serverURL + "/order/add", order, Boolean.class)) {
            result.setCode(200).setMessage("添加成功");
        }
        return result;
    }

    @GetMapping("/list")
    public CommonResult listOrder() {
        log.debug(serverURL);
        CommonResult result = new CommonResult().setCode(444).setMessage("查询失败");
        List forObject = restTemplate.getForObject(serverURL + "/order/list", List.class);
        if (forObject != null && forObject.size() > 0) {
            result.setCode(200).setMessage("查询成功").setData(forObject);
        } else if (forObject.size() == 0) {
            result.setCode(200).setMessage("没有查询到结果");
        }

        return result;
    }

}
